/*
 * AddNamesDialog.java
 *
 * Created on 27. August 2008, 19:31
 */

package jnamechooser;

import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 *
 * @author  uli
 */
public class AddNamesDialog extends javax.swing.JDialog {

    /** Creates new form AddNamesDialog */
    public AddNamesDialog(java.awt.Frame parent, boolean modal, PreparedStatement[] statements) {
        super(parent, modal);
        initComponents();
        stmts = statements;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        typeButtonGroup = new javax.swing.ButtonGroup();
        namesScrollPane = new javax.swing.JScrollPane();
        namesField = new javax.swing.JTextPane();
        surnameRB = new javax.swing.JRadioButton();
        maleFirstNamesRB = new javax.swing.JRadioButton();
        femaleFirstNamesButton = new javax.swing.JRadioButton();
        okButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Add names");

        namesScrollPane.setViewportView(namesField);

        typeButtonGroup.add(surnameRB);
        surnameRB.setSelected(true);
        surnameRB.setText("Surname");

        typeButtonGroup.add(maleFirstNamesRB);
        maleFirstNamesRB.setText("Male names");

        typeButtonGroup.add(femaleFirstNamesButton);
        femaleFirstNamesButton.setText("Female names");

        okButton.setText("OK");
        okButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                okButtonMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(105, 105, 105)
                .addComponent(surnameRB, javax.swing.GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE)
                .addGap(122, 122, 122))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(maleFirstNamesRB, javax.swing.GroupLayout.DEFAULT_SIZE, 139, Short.MAX_VALUE)
                        .addGap(33, 33, 33)
                        .addComponent(femaleFirstNamesButton)
                        .addGap(27, 27, 27))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(okButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 301, Short.MAX_VALUE)
                            .addComponent(namesScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 301, Short.MAX_VALUE))
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(surnameRB)
                .addGap(13, 13, 13)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(maleFirstNamesRB)
                    .addComponent(femaleFirstNamesButton))
                .addGap(18, 18, 18)
                .addComponent(namesScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(okButton)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void okButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_okButtonMouseClicked
    
    try
    {
           if(surnameRB.isSelected())
            {
                String[] s = namesField.getText().split("\n");                
                for (int i = 0;i < s.length;i++)
                    {
                        stmts[0].setString(1, s[i]);
                        stmts[0].executeUpdate();
                    }
            }
           else if(maleFirstNamesRB.isSelected())
            {
                String[] s = namesField.getText().split("\n");                
                for (int i = 0;i < s.length;i++)
                    {
                        stmts[1].setString(1, s[i]);
                        stmts[1].setBoolean(2, false);
                        stmts[1].executeUpdate();
                    }
            }
           else //femaleFirstNamesRB.isSelected() == true
            {
                String[] s = namesField.getText().split("\n");                
                for (int i = 0;i < s.length;i++)
                    {
                        stmts[1].setString(1, s[i]);
                        stmts[1].setBoolean(2, true);
                        stmts[1].executeUpdate();
                    }
            }
    }
    catch (SQLException ex) {ex.printStackTrace();}
}//GEN-LAST:event_okButtonMouseClicked

    private PreparedStatement[] stmts;

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JRadioButton femaleFirstNamesButton;
    private javax.swing.JRadioButton maleFirstNamesRB;
    private javax.swing.JTextPane namesField;
    private javax.swing.JScrollPane namesScrollPane;
    private javax.swing.JButton okButton;
    private javax.swing.JRadioButton surnameRB;
    private javax.swing.ButtonGroup typeButtonGroup;
    // End of variables declaration//GEN-END:variables

}
